package main

import (
	"errors"
	"fmt"
	"math"
)

func greeting() {
	println("Hello, world!")
}

func Multiply3Nums(a int, b int, c int) int {
	return a * b * c
}
func PrintValues() {
	fmt.Printf("num = %d, 2x num = %d, 3x num = %d\n", num, numx2, numx3)
}
func getX2AndX3(input int) (int, int) {
	return 2 * input, 3 * input
}
func getX2AndX3_2(input int) (x2 int, x3 int) {
	x2 = 2 * input
	x3 = 3 * input
	return
}

var num int = 10
var numx2, numx3 int

// 命名返回值
func caclc(input float64) (float64, error) {
	if input < 0 {
		return 0, errors.New("input must be positive")
	} else {
		return math.Sqrt(input), nil
	}
}

// 非命名返回值
func caclc1(input float64) (result float64, err error) {
	if input < 0 {
		err = fmt.Errorf("input must be positive")
		return
	} else {
		result = math.Sqrt(input)
		return
	}
}

func ThreeValues() (int, int, float64) {
	return 1, 2, 3.0
}

func MinMax(a int, b int) (min int, max int) {
	if a > b {
		max = a
		min = b
	} else {
		max = b
		min = a
	}
	return
}

// 改变外部变量
func Multiply(a, b int, reply *int) {
	*reply = a * b
}

// 传递变长参数  找出证书切片中的最小值
func minn(s ...int) int {
	if len(s) == 0 {
		return 0
	}
	min := s[0]
	for _, v := range s {
		if v < min {
			min = v
		}
	}
	return min
}

// 写一个函数，该函数接受一个变长参数并对每个元素进行换行打印
func test1(s ...int) int {
	if len(s) == 0 {
		return 0
	}
	for _, v := range s {
		fmt.Printf("%d\n", v)
	}
}

func main() {
	greeting()
	fmt.Printf("Multiply a * b * c = %d\n", Multiply3Nums(1, 2, 3))
	numx2, numx3 = getX2AndX3(num)
	PrintValues()
	numx2, numx3 = getX2AndX3_2(num)
	PrintValues()
	numx2, err := caclc(9.0)
	fmt.Printf("numx2 = %f, err = %v\n", numx2, err)
	numx2, err = caclc1(9.0)
	fmt.Printf("numx2 = %f, err = %v\n", numx2, err)
	num, numx3, _ = ThreeValues()
	fmt.Printf("num = %d, numx3 = %d\n", num, numx3)
	var min, max int
	min, max = MinMax(1, 2)
	fmt.Printf("min = %d, max = %d\n", min, max)
	n := 0
	reply := &n
	Multiply(1, 2, reply)
	fmt.Printf("n = %d\n", n)
	x := minn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
	fmt.Printf("x = %d\n", x)
	slice := []int{11, 12, 13, 14, 14, 5, 555}
	x = minn(slice...)
	fmt.Printf("x = %d\n", x)
}
